package cn.fz.ssedemo.utils;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;

/**
 * servlet相关工具类
 */
public class ServletUtils {
    private static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!(requestAttributes instanceof ServletRequestAttributes servletRequestAttributes)) {
            return null;
        }
        return servletRequestAttributes.getRequest();

    }

    /**
     * 页面输出
     * 
     * @param response
     *            响应对象
     * @param obj
     *            要输出的对象
     */
    public static void responseWriter(HttpServletResponse response, Object obj) {
        String errStr = JSONUtil.toJsonStr(obj);
        response.setContentType("application/json;charset=utf-8");
        try {
            response.getWriter().write(errStr);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
